package com.sgb.capital.viewmodel;

import android.app.Activity;

import com.sgb.capital.api.CapitalManager;
import com.sgb.capital.api.CommodityManager;
import com.sgb.capital.model.BaseEntity;
import com.sgb.capital.model.CommodityBean;
import com.sgb.capital.utils.DialogHelper;
import com.sgb.capital.view.widget.MToast;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者:张磊
 * 日期:2021/11/17 0017
 * 说明:商品
 */
public class CommodityModel extends ViewModel {
    public MutableLiveData<CommodityBean> mCommodityBean = new MutableLiveData<>();

    /**
     * 发起的询价
     *
     * @param activity
     */
    public void enquiryList(Activity activity, int limit, int page) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        CommodityManager.getInstance().getCommodityAPI().enquiryList(limit, page).enqueue(new Callback<BaseEntity<CommodityBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<CommodityBean>> call, Response<BaseEntity<CommodityBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mCommodityBean.setValue(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CommodityBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    /**
     * 收到的询价
     *
     * @param activity
     */
    public void receivedEnquiryList(Activity activity, int limit, int page) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        CommodityManager.getInstance().getCommodityAPI().receivedEnquiryList(limit, page).enqueue(new Callback<BaseEntity<CommodityBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<CommodityBean>> call, Response<BaseEntity<CommodityBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mCommodityBean.setValue(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CommodityBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    /**
     * 发起的报价
     *
     * @param activity
     */
    public void publishQuotedEnquiryList(Activity activity, int limit, int page) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        CommodityManager.getInstance().getCommodityAPI().publishQuotedEnquiryList(limit, page).enqueue(new Callback<BaseEntity<CommodityBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<CommodityBean>> call, Response<BaseEntity<CommodityBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mCommodityBean.setValue(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CommodityBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    /**
     * 收到的报价
     *
     * @param activity
     */
    public void receivedQuotedList(Activity activity, int limit, int page) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        CommodityManager.getInstance().getCommodityAPI().receivedQuotedList(limit, page).enqueue(new Callback<BaseEntity<CommodityBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<CommodityBean>> call, Response<BaseEntity<CommodityBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mCommodityBean.setValue(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CommodityBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
} 